package com.itheima.controller;

import com.itheima.common.R;

import com.itheima.domain.OmsOrderDelivery;
import com.itheima.domain.dto.OrderMoneyDto;
import com.itheima.domain.dto.OrderReceiverDto;
import com.itheima.manager.OmsOrderManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class OmsOrderController {

    @Autowired
    OmsOrderManager omsOrderManager;

    //分页查询订单详情
    @GetMapping("/order/list")
    public R findByPage(String createTime, String orderSn, Integer orderType,
                        Integer pageNum, Integer pageSize, String receiverKeyword,
                        Integer sourceType, Integer status){

         return omsOrderManager.findByPage(createTime,orderSn,orderType,pageNum,pageSize,receiverKeyword,sourceType,status);
    }


    //批量删除订单(逻辑删除，将delete_status字段值改为1)
    @PostMapping("/order/delete")
    public R delete(@RequestParam List<Integer> ids) {
        Integer count = omsOrderManager.delete(ids);
        if (count == 0) {
            return R.error(404,"关闭订单失败!");
        }
        return R.success("删除成功!");
    }

    //批量关闭订单
    @PostMapping("/order/update/close")
    public R close(@RequestParam Map map) {
        omsOrderManager.close(map);
        return R.success("关闭订单成功!");
    }

    //批量发货
    @PostMapping("/order/update/delivery")
    public R delivery(@RequestBody List<OmsOrderDelivery> list) {
        omsOrderManager.delivery(list);
        return R.success("发货成功!");
    }


    //根据订单id获取订单详情
    @GetMapping("/order/{id}")
    public R findOrderDetails(@PathVariable Integer id) {
        return omsOrderManager.findById(id);
    }

    //备注订单
    @PostMapping("/order/update/note")
    public R note(@RequestParam Map<String, String> map) {
        Integer count = omsOrderManager.note(map);
        if (count == 0) {
            return R.error(404,"备注失败!");
        }
        return R.success("备注成功!");
    }

    //修改订单费用信息
    @PostMapping("/order/update/moneyInfo")
    public R updateOrderMoney(@RequestBody OrderMoneyDto orderMoneyDto){
        omsOrderManager.updateOrderMoney(orderMoneyDto);
        return R.success("操作成功");
    }

    //修改收货人信息
    @PostMapping("/order/update/receiverInfo")
    public R receiverInfo(@RequestBody OrderReceiverDto orderReceiverDto){
        omsOrderManager.updateReceiver(orderReceiverDto);
        return R.success("操作成功");
    }
}
